﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Sales;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_ITM_SO : RibbonForm
    {
        public FND_ITM_SO()
        {
            InitializeComponent();
        }

        #region Properties
        public string ConnectionString { get; set; }
        public string CustomerCode { get; set; }
        public string ItemCode { get; set; }
        public string Description { get; set; }
        public string UOM { get; set; }
        public double UnitPrice { get; set; }
        public string PriceListCode { get; set; }

        List<string> _ItemCodes = new List<string>();

        public List<string> ItemCodes
        {
            get { return _ItemCodes; }
            set { _ItemCodes = value; }
        }
        #endregion

        #region Function

        public void FindItemCode(string sItemCode)
        {
            txtSearch.Text = sItemCode;
            cboSearch.SelectedIndex = 0;
            AddressBookManager oBPCard = new AddressBookManager();

            string sPriceListCode;

            oBPCard.ConnectionString = this.ConnectionString;
            oBPCard.Open();

            sPriceListCode = oBPCard.GetPriceListCode(this.CustomerCode);

            oBPCard.Close();

            BindPriceList();

            cboPriceList.SelectedValue = sPriceListCode;

            btnSearch_Click(null, null);

            gridControl1.Focus();
        }


        public void FindItemName(string sItemName)
        {
            txtSearch.Text = sItemName;
            cboSearch.SelectedIndex = 0;
            AddressBookManager oBPCard = new AddressBookManager();

            string sPriceListCode;

            oBPCard.ConnectionString = this.ConnectionString;
            oBPCard.Open();

            sPriceListCode = oBPCard.GetPriceListCode(this.CustomerCode);

            oBPCard.Close();

            BindPriceList();

            cboPriceList.SelectedValue = sPriceListCode;
            cboSearch.SelectedIndex = 1;

            btnSearch_Click(null, null);

            gridControl1.Focus();
        }

        #endregion




        #region Initialize Order Form

       

        void BindPriceList()
        {
            PriceManager oPriceManager = new PriceManager();

            oPriceManager.ConnectionString = this.ConnectionString;
            oPriceManager.Open();

            cboPriceList.DisplayMember = "PRICELISTNAME";
            cboPriceList.ValueMember = "PRICELISTCODE";
            cboPriceList.DataSource = oPriceManager.GetPriceList();

            cboPriceList.Refresh();
            oPriceManager.Close();
        }

        #endregion

        private void FND_ITM_SO_Load(object sender, EventArgs e)
        {
            if (cboPriceList.Items.Count <= 0)
            {
                BindPriceList();
                cboSearch.SelectedIndex = 0;


                AddressBookManager oBPCard = new AddressBookManager();

                string sPriceListCode;

                oBPCard.ConnectionString = this.ConnectionString;
                oBPCard.Open();

                sPriceListCode = oBPCard.GetPriceListCode(this.CustomerCode);

                oBPCard.Close();

                cboPriceList.SelectedValue = sPriceListCode;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cboPriceList.SelectedIndex != -1)
            {
                ItemManager oManager = new ItemManager();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                string sPriceListCode = cboPriceList.SelectedValue.ToString();

                DataTable dt = new DataTable();

                if (cboSearch.SelectedIndex == 0)
                {
                    dt = oManager.BrowseSlsItemByCodeWithPrice(sPriceListCode, txtSearch.Text);
                }
                else
                {
                    dt = oManager.BrowseSlsItemByNameWithPrice(sPriceListCode, txtSearch.Text);
                }

                oManager.Close();

                gridControl1.DataSource = dt;
                gridControl1.Refresh();


            }
            else
            {
                MessageBox.Show("Please select pricelist first!!!", "Price List", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            this.PriceListCode = cboPriceList.SelectedValue.ToString();
            this.ItemCode = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"]).ToString();
            this.Description = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DESCRIPTION"]).ToString(); ;
            this.UOM = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UNITOFMEASURE"]).ToString();
            
            this.UnitPrice = Convert.ToDouble(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["UNITPRICE"]).ToString());

            
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvwSearched_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void lvwSearched_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
                gridControl1.Focus();
            }
        }


    }
}
